#include <SFML/Graphics/RenderWindow.hpp>
#include <nice2d/macros.h>
#include <nice2d/background.h>
#include <nice2d/imagedescriptor.h>
#include <nice2d/util.h>
#include <nice2d/tilemap.h>
#include <nice2d/widgets/helplistener.h>
#include <nice2d/components/imagemanager.h>
#include <nice2d/components/scenecamera.h>
#include <nice2d/piece/piecescene.h>
#include <nice2d/actions/colorizedaction.h>

#define SCENE_WIDTH  1.5*900.0f
#define SCENE_HEIGHT 1.5*640.0f

class UnitScene : public PieceScene
{
public:
    void onInitialed()override
    {
        setSceneBox({0.0f, 0.0f, 1600.0f, 1200.0f});
        setBackground(Background::create("./../data/images/ground.png", true));

        auto camera = GET_COMPONENT(this, SceneCamera);
        camera->setViewSize({SCENE_WIDTH, SCENE_HEIGHT});

        initTileMap();

        deployPieces();
        initControls();
    }
private:
    void initTileMap()
    {
        tileMap = TileMap::create(TileMapType::TileMapType_Hex);
        tileMap->init(24, 15, 112.0);
        tileMap->setTextVisible(true);

        tileMap->accept([](int32_t x, int32_t y, std::shared_ptr<Tile> tile) {
            tile->setOutlineColor(sf::Color(128, 128, 128));
            tile->setOutlineThickness(0.5f);
            tile->setWalkable(true);
        });

        addChild(tileMap);
        setTileMap(tileMap);
        tileMap->tileClicked.connect(std::bind(&UnitScene::tileClicked, this, std::placeholders::_1,
                                               std::placeholders::_2));
    }

    void deployPieces()
    {
        TextOption opt;
        opt.characterSize = 15;

        sf::Color spriteColor[] = {sf::Color::Red, sf::Color::Green};

        for (int i = 0; i < 12; i++) {
            auto piece = std::make_shared<Piece>(opt);
            piece->setName(std::wstring(L"Piece") + std::to_wstring(i + 1));
            piece->setHitPoint(rand() % 5 + 1);
            piece->setAttack(rand() % 5 + 1);
            piece->setDefense(rand() % 5 + 1);
            piece->setMovement(rand() % 5 + 1);
            piece->setBackgroundColor(sf::Color(140, 140, 140));
            piece->setSpriteColor(spriteColor[i % 2]);
            piece->setSize(64, 64);
            piece->addPieceIcon("./../data/icon/nato/units/infantry.png");
            piece->addPieceIcon("./../data/icon/nato/sizes/group.png");
            piece->setTextColor(sf::Color::White);
            addPiece(piece, rand() % 13, rand() % 9);
        }
    }

    void tileClicked(int32_t x, int32_t y)
    {
        if (sf::Mouse::isButtonPressed(sf::Mouse::Left)) {
            auto tile = tileMap->getTileByIndex(x, y);
            if (tile)
                GET_COMPONENT(this, SceneCamera)->setCenter(tile->getPosition());
        }
    }

    void initControls()
    {
        auto helper = std::make_shared<HelpListener>(L"click mouse left button to kill piece");
        registerMessageListener(helper);
    }
private:
    std::shared_ptr<TileMap> tileMap;
    std::shared_ptr<Piece> currentPiece;
};

DECLARE_MAIN_FUNCTION_WITH_SCENE(SCENE_WIDTH, SCENE_HEIGHT, "unit20", UnitScene)

